<template>
  <el-container>
    <el-header>
      <img src="../assets/logo.png" alt="" class="logo">
      <span>首页</span>
      <input type="button" value="注销" @click="quit">
    </el-header>
    <el-container>
      <el-aside :width="isCol?'64px':'200px'">
        <!-- 侧边栏菜单 -->
          <!-- 伸缩按钮 -->
          <p class="colBtn" @click="changeCol">|||</p>
          <!-- 侧边栏菜单 -->
          <el-menu
            background-color="#bbb"
            text-color="#333"
            active-text-color="#ffd04b"
            :unique-opened="true"
            :collapse="isCol"
            :collapse-transition="false"
            router
            :default-active="currentPath"
            >
            <!-- 一级菜单 -->
            <el-submenu :index="item.id + ''" v-for="item in userList" :key="item.id">
              <!-- 一级菜单模板 -->
              <template slot="title">
                <!-- 图标 -->
                <i :class="classList[item.id]"></i>
                <!-- 文本 -->
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级子菜单 -->
              <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="changeCurrentPath(subItem.path)">
                <!-- 二级菜单模板 -->
                <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-setting"></i>
                  <!-- 文本 -->
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>

        <!-- 侧边栏结束 -->
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  components:{},
  data(){
    return {
      // 左侧菜单
      userList:[],
      // 一级菜单图标
      classList:{
        "125":"iconfont icon-user",
        "103":"iconfont icon-3702mima",
        "101":"iconfont icon-shangpin",
        "102":"iconfont icon-danju",
        "145":"iconfont icon-baobiao",
      },
      // 伸缩标识符
      isCol:false,
      // 记录当前path路径
      currentPath:null,
    }
  },
  // 创建模板的时候就会执行
  created(){
    this.getUserList()
    // 拿到当前的path值,把值给到currentPath
    this.currentPath=window.sessionStorage.getItem('cPath')
  },
  mounted(){},
  watch:{},
  computed:{},
  methods:{
    quit(){
      // 清除所有的sessionStorage
      window.sessionStorage.clear()
      // 编程导航跳登录
      this.$router.push('/login')
    },
    // 渲染左侧菜单
    getUserList(){
      // console.log("渲染左侧菜单");
      // 发请求获取左侧菜单
      this.$axios.get('menus').then(val=>{
        // console.log(val);
        if(val.meta.status!=200){
          return this.$message.error('左侧列表请求失败!')
        }
        // console.log(val.data);
        this.userList=val.data;
      })
    },
    // 实现左侧导航伸缩
    changeCol(){
      this.isCol=!this.isCol
    },
    // 点击二级菜单存储当前path值到sessionStorage
    changeCurrentPath(path){
      // console.log(path);
      window.sessionStorage.setItem('cPath',path);
    }
  },
}
</script>
<style lang="css" scoped>
</style>